package connection;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Set;

import json.GsonImplements;

import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.util.JSON;

public class Connection {

	public Mongo m;
	public DB db;
	public DBCollection collection;

	public Connection() {

		try {

			System.out.println("MONGO DB CONNECTION");
			m = new Mongo("localhost", 27017);
			db = m.getDB("myData");

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (MongoException e) {
			e.printStackTrace();
		}
	}

	public void findRow() {
		BasicDBObject searchQuery = new BasicDBObject();
		searchQuery.put("id", 1001);

		//select * from users where name like '%m%'
		//db.users.find({"name": /.*m.*/}
		//db.users.find({"name": /m/})

		collection = db.getCollection("Pessoas");
		DBCursor cursor = collection.find(searchQuery);

		while (cursor.hasNext()) {
			System.out.println(cursor.next());
		}
	}

	public void insertRow() {
		BasicDBObject telefone = new BasicDBObject();
		telefone.put("foneResidencia", "44-35256622");
		telefone.put("foneCelular", "44-99124929");
		telefone.put("foneCelular", "44-99124929");

		BasicDBObject document = new BasicDBObject();
		document.put("id", 1001);
		document.put("nome", "Saulo Verussa");
		document.put("telefone", telefone);

		String pessoa = GsonImplements.execute();


		BasicDBObject p = (BasicDBObject) JSON.parse(pessoa);

		collection = db.getCollection("Pessoas");
		collection.insert(p);

	}

	public void removeOneRow() {
		BasicDBObject searchQuery = new BasicDBObject();
		searchQuery.put("id", 1001);

		collection = db.getCollection("Pessoas");
		DBObject doc = collection.findOne(searchQuery); //get first document

		collection.remove(doc);
	}

	public void removeRows() {
		BasicDBObject searchQuery = new BasicDBObject();
		searchQuery.put("id", 1001);

		collection = db.getCollection("Pessoas");
		DBCursor cursor = collection.find(searchQuery);
		int aux = 0;
		while (cursor.hasNext()) {
			System.out.println("Removido: " + ++aux);
			DBObject doc = cursor.next();
			collection.remove(doc);
		}
	}

	private String Enviar(Set<String> colls) {
		StringBuilder saida = new StringBuilder();
		for (String s : colls) {
			saida.append(s).append("<@br>");
		}
		return saida.toString();
	}

	private String Enviar(List<String> databaseNames) {
		StringBuilder saida = new StringBuilder();
		for (String s : databaseNames) {
			saida.append(s).append("<@br>");
		}
		return saida.toString();
	}

	public void removeAllDocs() {
		DBCursor cursor = collection.find();
		while (cursor.hasNext()) {
			collection.remove(cursor.next());
		}
	}

	public String showColletions() {
		System.err.println("Collentions Data Base to test");
		return Enviar(db.getCollectionNames());
	}

	public String showDataBases() {
		System.err.println("Data bases");
		return Enviar(m.getDatabaseNames());
	}

	public void uniqueKeyEnable() {
		collection = db.getCollection("Pessoas");
		BasicDBObject keys = new BasicDBObject();
		keys.put("id", 1);
		collection.ensureIndex(keys, "unique", true);

	}

	public void uniqueKeyDisable() {
		collection = db.getCollection("Pessoas");
		BasicDBObject keys = new BasicDBObject();
		keys.put("id", 5);
		collection.ensureIndex(keys, "unique", false);

	}
}
